package village.rainbow.dailyscene.uicontrol;

import village.rainbow.dailyscene.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
 * 이미지와 메모를 보여주는 View
 * @author K.O.J
 *
 */
public class SceneView extends RelativeLayout{
	
	private EditText mEditText;
	private ImageView mImageView;
	private Bitmap mBitmap;
	private boolean mHasImage= false;
	
	private Context mContext;
	
	// 아래는 그리기 도구
	private static final float TOUCH_TOLERANCE = 4;
	public Canvas mCanvas;
	
	public boolean hasSigned = false;
	
	float mX, mY;
	Path mPath;
	Paint mBitmapPaint;
	Paint mPaint;
	
	public SceneView(Context context) {
		super(context);
	}
	
	public SceneView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public SceneView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}
	
	public void init(Context context) {
		mContext = context;
		mEditText = (EditText)findViewById(R.id.edt_memo);
		mImageView = (ImageView)findViewById(R.id.imv_picture);
		mEditText.setTextColor(Color.WHITE);
		mEditText.setBackgroundColor(Color.argb(150, 120, 120, 120));
	}
	
	public void setText(String text){
		mEditText.setText(text);
	}
	
	public void setImageBitmap(Bitmap bitmap){
		mBitmap = bitmap;
		mImageView.setImageBitmap(bitmap);
		mHasImage = true;
		drawModeStart();
	}
	
	public void setImageDrawable(Drawable drawable){
		mImageView.setImageDrawable(drawable);
		mHasImage = true;
		drawModeStart();
	}
	
	private void drawModeStart() {
		mImageView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});
	}
	
	public boolean hasImage(){
		return mHasImage;
	}
	
	public Bitmap getImageBitmap(){
		if(mBitmap == null){
			mBitmap = ((BitmapDrawable)mImageView.getDrawable()).getBitmap();
		}
		return mBitmap;
	}
	
	public String getSceneText(){
		return mEditText.getText().toString();
	}
	
	@Override
	public void setOnClickListener(OnClickListener l) {
		mImageView.setClickable(true);
		mImageView.setOnClickListener(l);
		//super.setOnClickListener(l);
	}
	
	@Override
	public void setOnLongClickListener(OnLongClickListener l) {
		mImageView.setLongClickable(true);
		mImageView.setOnLongClickListener(l);
		//super.setOnLongClickListener(l);
	}

	public void removeImage() {
		mImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.camera));
		mHasImage = false;
	}
	
	
}
